#!/usr/bin/env bash

export COLORSCHEME=DoomOne

function run {
	if ! pgrep -x "$(basename "$1" | head -c 15)" 1>/dev/null; then
		"$@" &
	fi
}

function isRun {
	if pgrep "$(basename "$1")" 1>/dev/null; then
		return 0 # return true
	else
		return 1 # return false
	fi
}

### AUTOSTART PROGRAMS ###
# lxsession &
# picom --daemon &
#/usr/bin/emacs --daemon &
#nm-applet &
#"$HOME"/.screenlayout/layout.sh &
#sleep 1
#conky -c "$HOME"/.config/conky/qtile/01/"$COLORSCHEME".conf || echo "Couldn't start conky."

### UNCOMMENT ONLY ONE OF THE FOLLOWING THREE OPTIONS! ###
# 1. Uncomment to restore last saved wallpaper
#xargs xwallpaper --stretch < ~/.cache/wall &
# 2. Uncomment to set a random wallpaper on login
# find /usr/share/backgrounds/dtos-backgrounds/ -type f | shuf -n 1 | xargs xwallpaper --stretch &
# 3. Uncomment to set wallpaper with nitrogen
# nitrogen --restore &

# extra from ArcoLinux

#Set your native resolution IF it does not exist in xrandr
#More info in the script
#run $HOME/.config/qtile/scripts/set-screen-resolution-in-virtualbox.sh

#Find out your monitor name with xrandr or arandr (save and you get this line)
#xrandr --output VGA-1 --primary --mode 1360x768 --pos 0x0 --rotate normal
#xrandr --output DP2 --primary --mode 1920x1080 --rate 60.00 --output LVDS1 --off &
#xrandr --output LVDS1 --mode 1366x768 --output DP3 --mode 1920x1080 --right-of LVDS1
#xrandr --output HDMI2 --mode 1920x1080 --pos 1920x0 --rotate normal --output HDMI1 --primary --mode 1920x1080 --pos 0x0 --rotate normal --output VIRTUAL1 --off
#autorandr horizontal
# if xrandr | rg "HDMI-0 connected"; then
# 	xrandr --output DP-4 --off
# 	xrandr --output HDMI-0 --primary --mode 1920x1080 --rate 144.0
# fi
# if xrandr | rg "HDMI-0 connected"; then
#   xrandr --output HDMI-0 --primary --mode 1920x1080 --rate 144.0;
# 	xrandr --output DP-4 --mode 1920x1080 --rate 144.0;
# fi
xrandr --output HDMI-0 --primary --mode 1920x1080 --rate 144.0;
##changed via give-me-azerty-qtile
#setxkbmap be

#autostart ArcoLinux Welcome App
# run dex $HOME/.config/autostart/arcolinux-welcome-app.desktop &

#Some ways to set your wallpaper besides variety or nitrogen
# feh --bg-fill /usr/share/backgrounds/archlinux/arch-wallpaper.jpg &
# feh --bg-fill /usr/share/backgrounds/arcolinux/arco-wallpaper.jpg &
feh --bg-fill "$HOME/.config/qtile/background/wallhaven-cat_1920x1080.png"
#wallpaper for other Arch based systems
#feh --bg-fill /usr/share/archlinux-tweak-tool/data/wallpaper/wallpaper.png &

#start sxhkd to replace Qtile native key-bindings
run sxhkd -c ~/.sxhkdrc &

#starting utility applications at boot time
# run variety &
run nm-applet &
#run pamac-tray &
run xfce4-power-manager &
numlockx on &
# blueberry-tray &
/usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1 &

#starting user applications at boot time
# run volumeicon &
#run discord &
#nitrogen --restore &
#run caffeine -a &
#run vivaldi-stable &
#run firefox &
#run thunar &
#run dropbox &
#run insync start &
#run spotify &
#run atom &
#run telegram-desktop &
#run /usr/bin/octopi-notifier &
run fcitx
run warpd
run dunst

#锁屏
run xautolock -time 30 -locker "betterlockscreen -l"

# xmodmap要在xcape前面
[ -f "$HOME/.Xmodmap" ] && xmodmap "$HOME/.Xmodmap"

sleep 1
if ! isRun xcape; then
	xcape -e 'Control_L=Escape'
	xcape -e 'Shift_R=BackSpace'
	xcape -e 'Alt_L=Left'
	xcape -e 'Alt_R=Right'
	xcape -e 'Control_R=Down'
fi
